class Solution {

    int maxCount = 0;
    int leftEnd = 0, rightEnd = 0;

    public String longestPalindrome(String s) {
        char[] chars = s.toCharArray();
        if (chars.length == 0 || chars.length == 1) {
            return s;
        }
        int maxIndex = chars.length - 1;
        double currentIndex = 0.6F;
        float step = 0.5F;

        do {
            this.check((int) Math.floor(currentIndex - step), (int) currentIndex + 1, chars);
            currentIndex = currentIndex + step;
        } while (Math.round(currentIndex) < maxIndex + 1);
        return s.substring(this.leftEnd, this.rightEnd + 1);
    }


    public void check(int leftStart, int rightStart, char[] chars) {
        int count = 0, currentLeftEnd = 0, currentRightEnd = 0;
        int leftLen = leftStart - (-1);
        int rightLen = chars.length - rightStart;
        for (int i = 0; i < (leftLen < rightLen ? leftLen : rightLen); i++) {
            if (chars[leftStart - i] != chars[rightStart + i]) {
                break;
            }
            ++count;
            currentLeftEnd = leftStart - i;
            currentRightEnd = rightStart + i;
        }
        if (count > maxCount || (count == maxCount && leftStart+2==rightStart)) {
            maxCount = count;
            leftEnd = currentLeftEnd;
            rightEnd = currentRightEnd;
        }
    }

}
